package com.my.cd.proxy.candymachine;

import java.util.Random;

/**
 * 状态抽象接口实现：插入硬币状态类
 */
public class HasCoinState implements State {

    private transient CandyMachine candyMachine;//持有的糖果机对象，rmi中不用序列化

    public HasCoinState(CandyMachine candyMachine) {
        this.candyMachine = candyMachine;
    }

    @Override
    public void insertCoin() {
        System.out.println("you can't insert another coin!");

    }

    @Override
    public void returnCoin() {
        System.out.println("coin return!");
        candyMachine.setState(candyMachine.onReadyState);
    }

    @Override
    public void turnCrank() {
        System.out.println("crank turn...!");
        Random ranwinner = new Random();
        int winner = ranwinner.nextInt(10);
        if (winner == 0) {
            candyMachine.setState(candyMachine.winnerState);
        } else {
            candyMachine.setState(candyMachine.soldState);
        }
    }

    @Override
    public void dispense() {
    }

    @Override
    public void printState() {
        System.out.println("***HasCoinState***");

    }

    @Override
    public String getStateName() {
        return "HasCoin State";
    }

}
